import * as obj from "../public.js";
import { codeF } from "./codeAjax.js";
import { regF } from "./registerAjax.js";
import'./showH.js';

const o = obj.default;
const un = o.$('.un');
const tel = o.$('.tel');
const pw = o.$('.pw');
const pwR = o.$('.pwR');
// 创建集合收集所有inp框是否为true
const falg = {};
// 用户名正则
const regUn = /^[a-zA-Z0-9_-]{4,16}$/;
// 验证用户名
un.onchange = function () {
  if (regUn.test(un.value)) {
    un.parentNode.nextElementSibling.innerHTML = '√'
    // 用户名验证正确 添加un为true
    falg.un = true
  } else {
    un.parentNode.nextElementSibling.innerHTML = '用户名不正确'
    // 用户名验证错误，如果有un更改为false，没添加un为false
    falg.un = false;
  }
}



// 手机号正则
const regTel = /^1[2-9]\d{9}$/;
// 验证手机号格式
tel.onchange = function () {
  if (regTel.test(tel.value)) {
    tel.parentNode.nextElementSibling.innerHTML = '√'
    falg.tel = true
  } else {
    tel.parentNode.nextElementSibling.innerHTML = '手机号格式不对'
    // 用户名验证错误，如果有un更改为false，没添加un为false
    falg.tel = false
  }
}




//密码正则，至少包含字母、数字，1-9位
const pwReg = /^(?=.*[a-zA-Z])(?=.*\d).{6,15}$/;
// 验证密码
pw.onchange = function () {
  if (pw.value !== '') {
    if (pwReg.test(pw.value)) {
      pw.parentNode.nextElementSibling.innerHTML = '√'
      falg.pw = true
    } else {
      pw.parentNode.nextElementSibling.innerHTML = '密码格式不对,至少包含字母、数字,6-15位  '
      // 用户名验证错误，如果有un更改为false，没添加un为false
      falg.pw = false
    }
    if (pwR.value) {
      if (pwR.value === pw.value) {
        pwR.parentNode.nextElementSibling.innerHTML = '√'
        falg.pwR = true
      } else {
        pwR.parentNode.nextElementSibling.innerHTML = '重复密码不对'
        // 用户名验证错误，如果有un更改为false，没添加un为false
        falg.pwR = false
      }
    }
  }
}




// 验证两次密码是否一样
pwR.onchange = function () {
  if (pw.parentNode.nextElementSibling.innerHTML === '√') {
    if (pwR.value === pw.value) {
      pwR.parentNode.nextElementSibling.innerHTML = '√'
      falg.pwR = true
    } else {
      pwR.parentNode.nextElementSibling.innerHTML = '重复密码不对'
      // 用户名验证错误，如果有un更改为false，没添加un为false
      falg.pwR = false
    }
  }
}
o.$('.form').onclick = function (eve) {
  const e = eve || window.event;
  const target = e.target || e.srcElement;
  if (target.tagName === 'I') {
    location.href = 'http://10.11.55.120:3000/html/login.html';
    console.log(target.tagName)
  }
  if (target.className === 'getCode') {
    if(falg.tel){
      codeF()
    }else{
      alert('手机号不正确')
    }
  }
  if (target.className === 'reg') {
    let num = 0;
    let f;
    for (let p in falg) {
      f = falg[p];
      num++;
    }
    if (num === 4 && f === true) {
      regF(e)
    }
  }
  if(target.className.indexOf('show') !== -1){
    const show = target.previousElementSibling
    if(show.type === 'password'){
      show.type = 'text';
      target.className = 'iconfont icon-yanjing_xianshi_o show'
    }else{
      show.type = 'password';
      target.className = 'iconfont icon-yanjing_yincang_o show'
    }
  }
}